package com.cy.kotlin2test

import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.cy.kotlin2test.Parent.SubClass.staticMethod
import com.cy.kotlin2test.ui.theme.Kotlin2TestTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        setContent {
            Kotlin2TestTheme {
                Scaffold( modifier = Modifier.fillMaxSize() ) { innerPadding ->
                    Greeting(
                        name = "Android",
                        modifier = Modifier.padding(innerPadding)
                    )
                }
            }
        }

//        Parent.SubClass()
        //父类--静态初始化块1
        //父类--静态变量
        //父类--静态初始化块2
        //子类--静态变量
        //子类--静态初始化块
        //子类--静态方法
//        Parent.SubClass.s_StaticField
        ParentKt.SubClassKt()
    }

    var textView:TextView? = null

    private fun init(parent: ViewGroup) {
        var list: ArrayList<String>? = null
        var list2: ArrayList<String> = ArrayList<String>()
        var result = with(list) {
            this?.add("?")
            this?.remove("?")
            println("${this?.size}")
            //最后一行返回结果
            5
        }
        with(list2){
            //可以省略this指代
            add("?")
        }


        var result1 = list?.run {
            add("?")
            //可以用this指代或不用
            this.remove("?")
            println("${this.size}")
            //最后一行返回结果
            5
        }

        var text1 = TextView(this).let {
            //必须用it指代，不能像apply那样直接写属性
            it.textSize = 10f
            println(it)
            //最后一行作为返回值
            it.text
        }

        var textView1 = TextView(this).also {
            //必须用it指代
            it.text = ""
            //返回T
        }

        textView?.apply {  }

        parent.addView(
            TextView(this)
                .apply {
                    // T.apply{}，返回T
                    this.text = ""
                    // 可以不用 this
                    gravity = Gravity.CENTER
                }.apply {
                    val outerTextView = this
                    this.setOnClickListener { view ->
                        println("当前 OnClickListener 对象: $this")
                        println("外部 TextView 对象: $outerTextView 或者写成 ${this@apply}")
                    }
                }
        )
    }
}




@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
    Greeting1()

}

@Composable
fun Greeting1() {
    Column {
        repeat(4) {
            Text(text = "Hello $it")
        }
    }
}

@Composable
fun GreetingPreview() {
    Kotlin2TestTheme {
        Greeting("Hello Android")
    }
}

var text1 by mutableStateOf("hello")
var showText by mutableStateOf(true)
/**
 * 后续某个时机对text进行更改 text = "lala"，Text组件会自动更新，利用属性委托来实现，这是kotlin特性
 * showText = false 比 设置是否可见更能提升性能
 */
@Preview(showBackground = true)
@Composable
fun TestPreview() {
    if (showText){
        Text(text = "Hello $text1")
    }
}

